package com.chaoxin.lovehelp.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chaoxin.lovehelp.service.RecipientService;
import com.chaoxin.lovehelp.common.enums.vo.allCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.chaoxin.lovehelp.entity.CategoryEntity;
import com.chaoxin.lovehelp.service.CategoryService;
import com.chaoxin.lovehelp.common.utils.PageUtils;
import com.chaoxin.lovehelp.common.utils.R;



/**
 * 
 *
 * @author honghu
 * @email 3027708213@qq.com
 * @date 2024-02-04 22:13:14
 */
@RestController
@CrossOrigin
@RequestMapping("lovehelp/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RecipientService recipientService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 下拉框
     */
    @RequestMapping("/allcategory")
    public R getAllCategory(){
        List<CategoryEntity> categorys = categoryService.getAllCategory();
        return R.ok().put("categorys",categorys);
    }
    /**
     * 单个类别信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		CategoryEntity category = categoryService.getById(id);
        allCategoryVo vo = new allCategoryVo();
        BeanUtils.copyProperties(category,vo);
        vo.setPersonsNum(recipientService.getPersonsNum(vo.getId()));
        return R.ok().put("category", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
        category.setIsDisplay(1);
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",category.getName());
        List<CategoryEntity> list = categoryService.list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            categoryService.save(category);
            return R.ok();
        }else return R.error(300,"该类别已存在。");



    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        QueryWrapper<CategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("name",category.getName());
        List<CategoryEntity> list = categoryService.list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            CategoryEntity category1 = categoryService.getById(category.getId());
            category1.setName(category.getName());
            categoryService.updateById(category1);

            return R.ok();
        }else return R.error(300,"该类别已存在。");

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		categoryService.removeIds(Arrays.asList(ids));

        return R.ok();
    }

}
